home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / chrome / calendar.jar / content / calendar / publish.js < prev    next >
Text File  |  2008-03-12  |  11KB  |  284 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 2001-2002
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   ArentJan Banck <ajbanck@planet.nl>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /**
  40.  * publishCalendarData
  41.  * Show publish dialog, ask for URL and publish all selected items.
  42.  */
  43. function publishCalendarData()
  44. {
  45.    var args = new Object();
  46.    
  47.    args.onOk =  self.publishCalendarDataDialogResponse;
  48.    
  49.    openDialog("chrome://calendar/content/publishDialog.xul", "caPublishEvents", 
  50.               "chrome,titlebar,modal,resizable", args );
  51. }
  52.  
  53. /**
  54.  * publishCalendarDataDialogResponse
  55.  * Callback method for publishCalendarData() that is called when the user
  56.  * presses the OK button in the publish dialog.
  57.  */
  58. function publishCalendarDataDialogResponse(CalendarPublishObject, aProgressDialog)
  59. {
  60.     publishItemArray(currentView().getSelectedItems({}), 
  61.                      CalendarPublishObject.remotePath, aProgressDialog);
  62. }
  63.  
  64. /**
  65.  * publishEntireCalendar
  66.  * Show publish dialog, ask for URL and publish all items from the calendar.
  67.  *
  68.  * @param cal   (optional) The calendar that will be published. If ommitted
  69.  *                         the user will be prompted to select a calendar.
  70.  */
  71. function publishEntireCalendar(cal)
  72. {
  73.     if (!cal) {
  74.         var count = new Object();
  75.         var calendars = getCalendarManager().getCalendars(count);
  76.  
  77.         if (count.value == 1) {
  78.             // Do not ask user for calendar if only one calendar exists
  79.             cal = calendars[0];
  80.         } else {
  81.             // Ask user to select the calendar that should be published.
  82.             // publishEntireCalendar() will be called again if OK is pressed
  83.             // in the dialog and the selected calendar will be passed in. 
  84.             // Therefore return after openDialog().
  85.             var args = new Object();
  86.             args.onOk = publishEntireCalendar;
  87.             args.promptText = calGetString("calendar", "publishPrompt");
  88.             openDialog("chrome://calendar/content/chooseCalendarDialog.xul", 
  89.                        "_blank", "chrome,titlebar,modal,resizable", args);
  90.             return;
  91.         }
  92.     }
  93.  
  94.     var args = new Object();
  95.     var publishObject = new Object( );
  96.  
  97.     args.onOk =  self.publishEntireCalendarDialogResponse;
  98.  
  99.     publishObject.calendar = cal;
  100.  
  101.     // restore the remote ics path preference from the calendar passed in
  102.     var remotePath = cal.getProperty("remote-ics-path");
  103.     if (remotePath && remotePath.length && remotePath.length > 0) {
  104.         publishObject.remotePath = remotePath;
  105.     }
  106.  
  107.     args.publishObject = publishObject;
  108.     openDialog("chrome://calendar/content/publishDialog.xul", "caPublishEvents", 
  109.                "chrome,titlebar,modal,resizable", args );
  110.  
  111.     return;
  112. }
  113.  
  114. /**
  115.  * publishEntireCalendarDialogResponse
  116.  * Callback method for publishEntireCalendar() that is called when the user
  117.  * presses the OK button in the publish dialog.
  118.  */
  119. function publishEntireCalendarDialogResponse(CalendarPublishObject, aProgressDialog)
  120. {
  121.     // store the selected remote ics path as a calendar preference
  122.     CalendarPublishObject.calendar.setProperty("remote-ics-path", 
  123.                                            CalendarPublishObject.remotePath);
  124.  
  125.     var itemArray = [];
  126.     var getListener = {
  127.         onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
  128.         {
  129.             publishItemArray(itemArray, CalendarPublishObject.remotePath, aProgressDialog);
  130.         },
  131.         onGetResult: function(aCalendar, aStatus, aItemType, aDetail, aCount, aItems)
  132.         {
  133.             if (!Components.isSuccessCode(aStatus)) {
  134.                 aborted = true;
  135.                 return;
  136.             }
  137.             if (aCount) {
  138.                 for (var i=0; i<aCount; ++i) {
  139.                     // Store a (short living) reference to the item.
  140.                     var itemCopy = aItems[i].clone();
  141.                     itemArray.push(itemCopy);
  142.                 }  
  143.             }
  144.         }
  145.     };
  146.     aProgressDialog.onStartUpload();
  147.     var oldCalendar = CalendarPublishObject.calendar;
  148.     oldCalendar.getItems(Components.interfaces.calICalendar.ITEM_FILTER_ALL_ITEMS,
  149.                          0, null, null, getListener);
  150.  
  151. }
  152.  
  153. function publishItemArray(aItemArray, aPath, aProgressDialog) {
  154.     var outputStream;
  155.     var inputStream;
  156.     var storageStream;
  157.  
  158.     var icsURL = makeURL(aPath);
  159.  
  160.     var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  161.                               .getService(Components.interfaces.nsIIOService);
  162.  
  163.     var channel = ioService.newChannelFromURI(icsURL);
  164.     if (icsURL.schemeIs('webcal'))
  165.         icsURL.scheme = 'http';
  166.     if (icsURL.schemeIs('webcals'))
  167.         icsURL.scheme = 'https';
  168.  
  169.     switch(icsURL.scheme) {
  170.         case 'http':
  171.         case 'https':
  172.             channel = channel.QueryInterface(Components.interfaces.nsIHttpChannel);
  173.             break;
  174.         case 'ftp':
  175.             channel = channel.QueryInterface(Components.interfaces.nsIFTPChannel);
  176.             break;
  177.         case 'file':
  178.             channel = channel.QueryInterface(Components.interfaces.nsIFileChannel);
  179.             break;
  180.         default:
  181.             dump("No such scheme\n");
  182.             return;
  183.     }
  184.  
  185.     var uploadChannel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);
  186.     uploadChannel.notificationCallbacks = notificationCallbacks;
  187.  
  188.     storageStream = Components.classes["@mozilla.org/storagestream;1"]
  189.                                   .createInstance(Components.interfaces.nsIStorageStream);
  190.     storageStream.init(32768, 0xffffffff, null);
  191.     outputStream = storageStream.getOutputStream(0);
  192.  
  193.     var serializer = Components.classes["@mozilla.org/calendar/ics-serializer;1"]
  194.                                .createInstance(Components.interfaces.calIIcsSerializer);
  195.     serializer.addItems(aItemArray, aItemArray.length);
  196.     // Outlook requires METHOD:PUBLISH property:
  197.     var methodProp = getIcsService().createIcalProperty("METHOD");
  198.     methodProp.value = "PUBLISH";
  199.     serializer.addProperty(methodProp);
  200.     serializer.serializeToStream(outputStream);
  201.     outputStream.close();
  202.  
  203.     inputStream = storageStream.newInputStream(0);
  204.  
  205.     uploadChannel.setUploadStream(inputStream,
  206.                                   "text/calendar", -1);
  207.     try {
  208.         channel.asyncOpen(publishingListener, aProgressDialog);
  209.     } catch (e) {
  210.         var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
  211.                         .getService(Components.interfaces.nsIStringBundleService);
  212.         var props = sbs.createBundle("chrome://calendar/locale/calendar.properties");
  213.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  214.                                       .getService(Components.interfaces.nsIPromptService);
  215.         promptService.alert(null, calGetString("calendar", "errorTitle"),
  216.                             props.formatStringFromName('otherPutError',[e.message],1));
  217.     }
  218. }
  219.  
  220.  
  221. var notificationCallbacks =
  222. {
  223.     // nsIInterfaceRequestor interface
  224.     getInterface: function(iid, instance) {
  225.         if (iid.equals(Components.interfaces.nsIAuthPrompt)) {
  226.             // use the window watcher service to get a nsIAuthPrompt impl
  227.             var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  228.                                .getService(Components.interfaces.nsIWindowWatcher);
  229.             return ww.getNewAuthPrompter(null);
  230.         }
  231.         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  232.         return null;
  233.     }
  234. }
  235.  
  236.  
  237. var publishingListener =
  238. {
  239.     QueryInterface: function(aIId, instance)
  240.     {
  241.         if (aIId.equals(Components.interfaces.nsIStreamListener) ||
  242.             aIId.equals(Components.interfaces.nsISupports))
  243.             return this;
  244.  
  245.         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  246.         return null;
  247.     },
  248.  
  249.     onStartRequest: function(request, ctxt)
  250.     {
  251.     },
  252.  
  253.     onStopRequest: function(request, ctxt, status, errorMsg)
  254.     {
  255.         ctxt.wrappedJSObject.onStopUpload();
  256.  
  257.         var channel;
  258.         var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
  259.                         .getService(Components.interfaces.nsIStringBundleService);
  260.         var props = sbs.createBundle("chrome://calendar/locale/calendar.properties");
  261.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  262.                                       .getService(Components.interfaces.nsIPromptService);
  263.         try {
  264.             channel = request.QueryInterface(Components.interfaces.nsIHttpChannel);
  265.             dump(ch.requestSucceeded+"\n");
  266.         } catch(e) {
  267.         }
  268.         if (channel && !channel.requestSucceeded) {
  269.             promptService.alert(null, calGetString("calendar", "errorTitle"),
  270.                                 props.formatStringFromName('httpPutError',[channel.responseStatus, channel.responseStatusText],2));
  271.         }
  272.         else if (!channel && !Components.isSuccessCode(request.status)) {
  273.             // XXX this should be made human-readable.
  274.             promptService.alert(null, calGetString("calendar", "errorTitle"),
  275.                                 props.formatStringFromName('otherPutError',[request.status.toString(16)],1));
  276.         }
  277.     },
  278.  
  279.     onDataAvailable: function(request, ctxt, inStream, sourceOffset, count)
  280.     {
  281.     }
  282. }
  283.  
  284.